<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/24 15:14
 * Remark:
 */

namespace App\Models\Position;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;

class PositionModel extends BaseModel
{
    protected $table = TableConsts::CY_POSITION_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_POSITION_TABLE_PRIMARY_KEY;

    protected $attributes = [
        'status' => BaseConsts::STATUS_NORMAL,
    ];

    protected $fillable = [
        'position_id',
        'position_name',
        'department_id',
        'position_remark',
        'status',
        'created_at',
        'updated_at',
        'position_status',
        'position_menu',
    ];

    /**
     * 不需要走全局作用域
     */
    protected static function booted(){}

    public function isExistByCondition($conditionData){
        return $this->withoutGlobalScopes()
            ->firstOrnew($conditionData)->exists;
    }

    /**
     * 创建职位
     * @param $position
     * @return bool
     */
    public function createPosition($positionData): bool
    {
        return $this->save($positionData);
    }

    /**
     * 编辑职位
     * @param $positionData
     * @return mixed
     */
    public function editPosition($positionData): mixed
    {
        return $this->where('position_id',$positionData['position_id'])->update($positionData);
    }

    /**
     * 根据部门ID获取职位下拉
     * @param $departmentId
     * @param array $select
     * @return mixed
     */
    public function getPositionSelectByDepartmentId($departmentId, array $select = ["*"]): mixed
    {
        return $this->select($select)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->where('department_id',$departmentId)
            ->get();
    }

    /**
     * 通过部门ID获取职位菜单
     * @param $departmentId
     * @param string[] $select
     * @return mixed
     */
    public function getPositionMenuByDepartmentId($departmentId, array $select = ["*"]): mixed
    {
        return $this->select($select)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->where('department_id',$departmentId)
            ->get();
    }


    /**
     * 根据部门ID获取职位列表
     * @param $query
     * @param $pageSize
     * @return mixed
     */
    public function getPositionListByDepartmentId($query,$pageSize): mixed
    {
        return $query->latest()
            ->Paginate($pageSize);
    }

    /**
     * 批量删除职位
     * @param $positionIds
     * @param $data
     * @return mixed
     */
    public function bulkDeletePosition($positionIds,$data): mixed
    {
        return $this->whereIn('position_id',$positionIds)->update($data);
    }

    /**
     * 根据公司Id获取职位列表
     * @param $query
     * @param $pageSize
     * @return mixed
     */
    public function getPositionListByCompanyId($query,$pageSize): mixed
    {
        return $query->where('status',BaseConsts::STATUS_NORMAL)
            ->latest()
            ->Paginate($pageSize);
    }

    /**
     * 通过职位ID 获取职位信息
     * @param $positionId
     * @param string[] $select
     * @return mixed
     */
    public function getPositionInfoByPositionId($positionId, array $select = ["*"]): mixed
    {
        return $this->select($select)
            ->where('position_id',$positionId)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->first();
    }

    /**
     * 校验position
     * @param $positionId
     * @param $positionName
     * @param $departmentId

     */
    public function legPositionByEdit($positionId,$positionName,$departmentId)
    {
        return $this->where('position_id','!=',$positionId)
            ->where('position_name',$positionName)
            ->where('department_id',$departmentId)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->first();
    }







}